/*
************************************************************************************************************************
	Creates estimation dataset $workdata/edulevels with information on:
	- Social assistance (sa)

	Uses auxiliary datasets:
	- $temp/bef_red2_`year' (see "$data_do/bef_red")

	Registers: Variables (Years) 
	- DREAM: pnr, y_01-y_52(53) (2000-2006)

************************************************************************************************************************
*/

forvalues year = 2000/2006 {

if `year'==2000 local day1=mdy(1,3,2000)
if `year'==2001 local day1=mdy(1,1,2001)
if `year'==2002 local day1=mdy(12,31,2001)
if `year'==2003 local day1=mdy(12,30,2002)
if `year'==2004 local day1=mdy(12,29,2003)
if `year'==2005 local day1=mdy(1,3,2005)
if `year'==2006 local day1=mdy(1,2,2006)


********************************************
* Select population fulfilling selelction criteria of Baseline selection except for educational attainment
********************************************

use "$temp/BEF/bef_red2_`year'", clear
keep if alderl>=19 & alderl<=31

keep if ie_type==1 
keep if fm_markl==6
keep if fm_markf==6 
keep if d_childl==0 
keep if d_childf==0 
keep if civstl=="U" 
keep if civstf=="U" 

***************************************************************
* Merge with data with weekly public income transfers y_1-y_52(y_53).
***************************************************************

capture drop _merge
merge m:1 pnr using "$dat/dream`year'", keep(master match)
preserve
keep y_*
local num_weeks=c(k)
restore
display `num_weeks'
forvalues yvar= 1/9 {
rename y_0`yvar' y_`yvar'
}


forvalues dream_week = 1/`num_weeks' {
g age_`dream_week'= ((`day1'+(7*(`dream_week'))-foed_dag))/365.25
}

forvalues week = 1/`num_weeks'  {
g month_`week'=0
}

forvalues month = 1/12 {
forvalues week = 1/`num_weeks'  {

if `month'<12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<  ((mdy(`month'+1,1,`year') - foed_dag)/365.25) {
replace month_`week'=`month'
}

if `month'==12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<=  ((mdy(12,31,`year') - foed_dag)/365.25) {
replace month_`week'=`month'
}
}
}

***************************************************************
* Create weekly observations of public income transfer (y) in weeks 1-`num_weeks'.
* (wide to long)
* Save baseline_2_`year'
***************************************************************

expand `num_weeks'
sort pnr 
capture drop week
gen week=1
bysort pnr: replace week=sum(week)
capture drop age
gen age=0
capture drop month
gen month=0
capture drop y
gen y=0

forvalues week=1/`num_weeks' {
bysort pnr: replace age=age_`week' if week==`week'
bysort pnr: replace y=y_`week' if week==`week'
bysort pnr: replace month=month_`week' if week==`week'
}

drop y_* age_* month_*   _merge

********************************************
* Defining  social assistance (sa).
* Save edu_3_`year'
********************************************

capture drop sa
g sa=0
replace sa=1 if y>=130 & y<=139 ///
              | y>=140 & y<=149 ///
			  | y>=700 & y<=709 ///
			  | y>=710 & y<=719 ///
			  | y>=720 & y<=729 ///
			  | y>=730 & y<=739 


keep pnr y age month year koen edul eduf sa
capture drop e
g e=1
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
destring edul, replace
destring eduf, replace
collapse sa edul eduf (sum) e, by(pnr age_q)
capture drop year
g year=`year'
save "$temp/edu_3_`year'", replace
}

***************************************************
*Collect yearly dataset to one.
*Save edulevels.
***************************************************

use "$temp/edu_3_2000", clear
forvalues year=2001/2006 {
append using "$temp/edu_3_`year'"
}

save "$workdata/edulevels", replace